package school.xauat.inscription_sys.util;

import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.UUID;

/**
 * @program: gq_message_sys
 * @author:  zsy
 * @create: 2022-01-05 09:01
 **/
public class ImgFmtUtil {
    public static String projectImgPath = System.getProperty("user.dir") + File.separator + "image" + File.separator;

    static {
        File file = new File(projectImgPath);
        if (!file.exists()) {
            file.mkdirs();
        }
    }

    public static boolean isCorrect(String imageName) {
        String suffix = getImgSuffix(imageName);
        if ((".gif").equals(suffix) || (".GIF").equals(suffix) ||
                (".jpg").equals(suffix) || (".JPG").equals(suffix) ||
                (".PNG").equals(suffix) || (".png").equals(suffix)) {
            return true;
        }
        return false;
    }

    public static String getImgSuffix(String imageName) {
        return imageName.substring(imageName.lastIndexOf("."));
    }

    public static String getImgStorePath(MultipartFile img) {

        return projectImgPath + img.getOriginalFilename();
    }

    //将本地图片路径转换为前端能访问的路径
    public static String localUrlToWebUrl(String projectHost, String localUrl) {
        if (localUrl == null) return null;
        String realName = localUrl.replace(projectImgPath, "");
        return projectHost + realName;
    }
}
